/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkBuffer.h"
#include "MkUtil/MkThread.h"
#include <gtest/gtest.h>

TEST(MkBuffer, Buff)
{
    CMkBuffer tmpBuffer((void*)"123", 3);
    EXPECT_EQ(3, tmpBuffer.GetLength());
    EXPECT_STREQ("123", (Int8*)tmpBuffer.GetBuffer());

    CMkBuffer tmpBuffer2(tmpBuffer);
    EXPECT_EQ(3, tmpBuffer2.GetLength());
    EXPECT_STREQ("123", (Int8*)tmpBuffer2.GetBuffer());

    CMkBuffer Buf3(nullptr, 0);
}

TEST(MkBuffer, BufferList)
{
    CMkBuffer tmpBuffer((void*)"123", 3);
    CMkBufferList BufList;
    for (int i = 0; i < 100; i++) {
        BufList.Append(tmpBuffer);
    }

    for (int i = 0; i < 100; i++) {
        BufList.Append((void*)"123", 3, 1);
    }

    BufList.RemoveBegin(0);
    BufList.RemoveBegin(200);
    EXPECT_EQ(400, BufList.GetTatolLen());
    BufList.Append((void*)"123", 3, 0);
    BufList.RemoveBegin(1000);
    BufList.Append((void*)"123", 3, 1);
    EXPECT_EQ(3, BufList.GetBufferCount());
    EXPECT_NE(TRUE, BufList.Empty());
    MkListBuffer::const_iterator it = BufList.GetBufferList().begin();
    for (int i = 0; it != BufList.GetBufferList().end(); it++, i++) {
        if (0 == i) {
            EXPECT_EQ('1', ((Uint8*)it->GetBuffer())[0]);
        } else if (1 == i) {
            EXPECT_EQ('2', ((Uint8*)it->GetBuffer())[0]);
        } else if (2 == i) {
            EXPECT_EQ('3', ((Uint8*)it->GetBuffer())[0]);
        }
    }
    BufList.Clear();
    EXPECT_EQ(BufList.GetBufferList().size(), 0);
    EXPECT_EQ(BufList.GetTatolLen(), 0);
}

TEST(MkBuffer, InsertHeadBufferList)
{
    CMkBufferList BufferList1;
    Uint32 ErrorCode = BufferList1.Append((void*)"67", 2, 1);
    EXPECT_EQ(NoneError, ErrorCode);
    CMkBufferList BufferList3;
    ErrorCode = BufferList3.Append((void*)"89", 2);
    EXPECT_EQ(NoneError, ErrorCode);
    CMkBufferList BufferList2;
    ErrorCode = BufferList2.Append(BufferList3);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.InsertHead(BufferList1);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.InsertHead((void*)"5", 1, 0);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.InsertHead((void*)"34", 2, 1);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.InsertHead(CMkBuffer((void*)"12", 2));
    EXPECT_EQ(NoneError, ErrorCode);
    CMkBufferList GetList;
    ErrorCode = BufferList2.GetBuffer(0, 3, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(0, 4, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(0, 5, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(0, 6, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(0, 8, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(1, 3, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(2, 3, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(4, 3, GetList);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = BufferList2.GetBuffer(10, 3, GetList);
    EXPECT_NE(NoneError, ErrorCode);
    ErrorCode = BufferList2.RemoveBegin(100);
    EXPECT_EQ(NoneError, ErrorCode);
}

TEST(MkBuffer, StorageBufferList)
{
    CMkTime FirstBeginTime;
    CMkTime SecondBeginTime;
    CMkBufferList BufferList;
    BufferList.Append("123", 3);
    CMkStorageBufferList StorageBufferList;
    CMkTime BeginTime;
    Uint32  ErrorCode = StorageBufferList.GetBeginBuildTime(BeginTime);
    StorageBufferList.Append(BufferList);
    CMkThread::ThreadSleep(10);
    StorageBufferList.Append(CMkBuffer("12", 2));
    EXPECT_EQ(5, StorageBufferList.GetTatolLen());
    StorageBufferList.GetBeginBuildTime(FirstBeginTime);
    StorageBufferList.RemoveBegin(1);
    EXPECT_EQ(4, StorageBufferList.GetTatolLen());
    StorageBufferList.RemoveBegin(1);
    EXPECT_EQ(3, StorageBufferList.GetTatolLen());
    StorageBufferList.RemoveBegin(1);
    EXPECT_EQ(2, StorageBufferList.GetTatolLen());
    StorageBufferList.GetBeginBuildTime(SecondBeginTime);
    EXPECT_EQ(FALSE, FirstBeginTime == SecondBeginTime);
    CMkBufferList GetList;
    StorageBufferList.GetBufferList(GetList);
    EXPECT_EQ(1, GetList.GetBufferList().size());
    MkListStorageBuffer::const_iterator it = StorageBufferList.GetStorageBufferList().begin();
    for (; it != StorageBufferList.GetStorageBufferList().end(); it++) {
        EXPECT_EQ((*it)->GetLength(), 2);
        EXPECT_EQ(((const Uint8*)(*it)->GetBuffer())[0], '1');
    }
    StorageBufferList.Append((void*)"123", 3, 1);
    StorageBufferList.RemoveBegin(100);
    StorageBufferList.Append((void*)"123", 3, 1);
    StorageBufferList.Clear();
    EXPECT_EQ(StorageBufferList.GetStorageBufferList().size(), 0);
    EXPECT_EQ(StorageBufferList.GetTatolLen(), 0);
}
